using System;

enum AccountType
{
    Savings,
    CreditCard
}

// 银行
class Bank
{
	public string bankName { get; set; }
	public Bank(string name)
	{
		this.bankName = name;
	}
}

// 账户
class Account : Bank
{
	public AccountType AccountType { get; set; }
	private decimal balance;
	private string holderName;

	public decimal Balance
	{
		get { return balance; }
	}

	public string HolderName
	{
		get { return holderName; }
	}

	public Account(AccountType accountType, string bankName, string holderName, decimal initialBalance) : base(bankName)
	{
		this.holderName = holderName;
		this.balance = initialBalance;
		this.AccountType = accountType;
	}

	public void Deposit(decimal amount)
	{
		if (amount > 0)
		{
			balance += amount;
		}
		else
		{
			Console.WriteLine("存款金额必须大于0!");
		}
	}

	public virtual void Withdraw(decimal amount)
	{
		if (amount > 0 && amount <= balance)
		{
			balance -= amount;
		}
		else
		{
			Console.WriteLine("取款金额无效或余额不足!");
		}
	}

}

class CreditCardAccount : Account
{
	public decimal creditLimit { get; private set; }

	public CreditCardAccount(AccountType accountType, string bankName, string holderName, decimal initialBalance, decimal creditLimit)
		: base(accountType, bankName, holderName, initialBalance)
	{
		this.creditLimit = creditLimit;
	}

	public override void Withdraw(decimal amount)
	{
		decimal newBalance = Balance - amount;
		if (newBalance < -creditLimit)
		{
			Console.WriteLine("取款金额超过信用额度");
			return;
		}
		base.Withdraw(amount);
	}
}

//自定义异常
class BadCashException : Exception
{
	public BadCashException(string message) : base(message) { }
}

class BigMoneyFetchedEventArgs : EventArgs
{
	public Account uAccount { get; }
	public decimal uAmount { get; }

	public BigMoneyFetchedEventArgs(Account account, decimal amount)
	{
		this.uAccount = account;
		this.uAmount = amount;
	}
}

//ATM

delegate void BigMoneyFetchedEventHandler(object sender, BigMoneyFetchedEventArgs e);

class ATM
{
	private Random random = new Random();

	public event BigMoneyFetchedEventHandler BigMoneyFetched;

	protected virtual void OnBigMoneyFetched(BigMoneyFetchedEventArgs e)
	{
		BigMoneyFetched(this, e);
	}

	public void WithdrawMoney(Account account, decimal amount)
	{
		try
		{
			if (random.NextDouble() < 0.3)
			{
				throw new BadCashException("检测到坏钞");
			}

			account.Withdraw(amount);

			if (amount > 10000)
			{
				OnBigMoneyFetched(new BigMoneyFetchedEventArgs(account, amount));
			}
		}
		catch (Exception ex)
		{
			Console.WriteLine($"发生错误: {ex.Message}");
		}
	}
}

class Program
{
	static void Main(string[] args)
	{
		Bank bank = new Bank("示例银行");
		Account account = new CreditCardAccount(AccountType.CreditCard, bank.bankName, "张三", 10000, 5000);

		ATM atm = new ATM();
		atm.BigMoneyFetched += Atm_BigMoneyFetched;

		try
		{
			atm.WithdrawMoney(account, 15000);
		}
		catch (Exception ex)
		{
			Console.WriteLine($"操作失败: {ex.Message}");
		}
	}

	private static void Atm_BigMoneyFetched(object sender, BigMoneyFetchedEventArgs e)
	{
		Console.WriteLine($"警告: 账号 {e.uAccount.HolderName} 取款 {e.uAmount} 元,超过10000元!");
	}
}